package oakclub.thrift;

import org.apache.thrift.TProcessor;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThriftServer extends Thread
{
    private TProcessor processor;
    private int port;
    private final Logger logger = LoggerFactory.getLogger(ThriftServer.class);

    public ThriftServer(TProcessor processor, int port)
    {
        this.processor = processor;
        this.port = port;
    }

    public void run()
    {
        TServerSocket serverTransport = null;
        try
        {
            serverTransport = new TServerSocket(this.port);
        }
        catch(Exception e)
        {
            this.logger.error("Start server failed", e);
            return;
        }
        if(serverTransport != null)
        {
            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(this.processor));
            this.logger.info("STARTING SERVER ON PORT {} ...", this.port);
            server.serve();
        }
        else
        {
            this.logger.error("server transport null");
        }
    }
}
